//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.zz.mxhidfingerdriver;

import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;

import com.fingerUsb.USBComm;

import org.zz.protocol.MXCommand;
import org.zz.protocol.MXLog;
import org.zz.tool.zzStringTrans;

import java.util.Calendar;

public class MXHidFingerComm {
    private boolean m_bCancelGetImage = false;
    private USBComm m_usbBase;
    private Handler m_fHandler = null;

    public MXHidFingerComm(USBComm m_usbBase) {
        this.m_fHandler = null;
        this.m_usbBase = m_usbBase;
    }

    public String getDevAttribute() {
        return this.m_usbBase.getDevAttribute();
    }

    public int mxGetDevNum() {
        return this.m_usbBase.getDevNum(MXCommand.VENDORID, MXCommand.PRODUCTID);
    }

    public int mxGetDevVersion(byte[] bVersion) {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_READ_VERSION;

        byte iSendLen = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        int var12 = this.sendPacket(bCmd, bSendBuf, iSendLen);
        if (var12 != 0) {
            this.m_usbBase.closeDev();
            return var12;
        } else {
            byte[] bResult = new byte[1];
            int[] iRecvDataLen = new int[1];
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            var12 = this.recvPacket(bResult, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT);
            if (var12 != 0) {
                this.m_usbBase.closeDev();
                return var12;
            } else {
                this.m_usbBase.closeDev();

                for (int i = 0; i < iRecvDataLen[0]; ++i) {
                    bVersion[i] = bRecvBuf[i];
                }

                return 0;
            }
        }

    }

    public int mxGetImage(byte[] bImgBuf, int iImgLen, int iTimeOut, int iFlagLeave) {
        boolean iRet = false;
        MXLog.SendMsg("打开设备");

            MXLog.SendMsg("探测手指，红灯亮");
          int  iRet1 = this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_ON);
            if (iRet1 != 0) {
                this.m_usbBase.closeDev();
                return iRet1;
            } else {
                MXLog.SendMsg("开启FPC传感器");
                iRet1 = this.zzStartFPC();
                if (iRet1 != 0) {
                    this.m_usbBase.closeDev();
                    return iRet1;
                } else {
                    this.m_bCancelGetImage = false;
                    long duration = -1L;
                    Calendar time1 = Calendar.getInstance();
                    MXLog.SendMsg("探测手指");
                    iRet1 = this.zzDetectFinger();

                    Calendar time2;
                    while (iRet1 == 1 && duration <= (long) iTimeOut && !this.m_bCancelGetImage) {
                        iRet1 = this.zzDetectFinger();
                        SystemClock.sleep(10L);
                        time2 = Calendar.getInstance();
                        if (iTimeOut != 0) {
                            duration = time2.getTimeInMillis() - time1.getTimeInMillis();
                        }
                    }

                    byte iRet2;
                    if (this.m_bCancelGetImage) {
                        this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                        this.zzStopFPC();
                        this.m_usbBase.closeDev();
                        iRet2 = -2;
                        return iRet2;
                    } else if (duration > (long) iTimeOut) {
                        this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                        this.zzStopFPC();
                        this.m_usbBase.closeDev();
                        iRet2 = -3;
                        return iRet2;
                    } else {
                        MXLog.SendMsg("读取图像");
                        iRet1 = this.zzReadImage();
                        if (iRet1 != 0) {
                            this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                            this.zzStopFPC();
                            this.m_usbBase.closeDev();
                            iRet2 = -4;
                            return iRet2;
                        } else {
                            MXLog.SendMsg("上传图像");
                            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
                            byte[] bRecvBuf = new byte[iRecvPackageSize];
                            iRet1 = this.zzUpPackAndroid(bRecvBuf, 0);
                            if (iRet1 == 0) {
                                MXLog.SendMsg("zzUpPackAndroid ok");
                                int[] iImgWidth = new int[1];
                                int[] iImgHeight = new int[1];
                                iRet1 = this.zzUpImageAndroid(bImgBuf, iImgWidth, iImgHeight);
                            } else {
                                MXLog.SendMsg("zzUpPackAndroid error,iRet=" + iRet1);
                                iRet1 = this.zzUpImage(bImgBuf, iImgLen);
                            }

                            if (iRet1 != 0) {
                                iRet1 = this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                                MXLog.SendMsg("zzLampOnOff iRet=" + iRet1);
                                iRet1 = this.zzStopFPC();
                                MXLog.SendMsg("zzStopFPC iRet=" + iRet1);
                                iRet1 = this.m_usbBase.closeDev();
                                iRet2 = -5;
                                return iRet2;
                            } else {
                                MXLog.SendMsg("探测到手指，红灯灭");
                                this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                                if (iFlagLeave == 1) {
                                    this.zzLampOnOff(MXCommand.LED_GREEN_LIGHT, MXCommand.LED_ON);
                                    duration = -1L;
                                    time1 = Calendar.getInstance();
                                    iRet1 = this.zzDetectFinger();

                                    while (iRet1 == 0 && duration <= (long) iTimeOut && !this.m_bCancelGetImage) {
                                        iRet1 = this.zzDetectFinger();
                                        SystemClock.sleep(10L);
                                        time2 = Calendar.getInstance();
                                        if (iTimeOut != 0) {
                                            duration = time2.getTimeInMillis() - time1.getTimeInMillis();
                                        }
                                    }

                                    this.zzLampOnOff(MXCommand.LED_GREEN_LIGHT, MXCommand.LED_OFF);
                                    if (this.m_bCancelGetImage) {
                                        this.zzStopFPC();
                                        this.m_usbBase.closeDev();
                                        iRet2 = -2;
                                        return iRet2;
                                    }

                                    if (duration > (long) iTimeOut) {
                                        this.zzStopFPC();
                                        this.m_usbBase.closeDev();
                                        iRet2 = -3;
                                        return iRet2;
                                    }
                                }

                                MXLog.SendMsg("关闭FPC传感器");
                                this.zzStopFPC();
                                MXLog.SendMsg("关闭设备");
                                this.m_usbBase.closeDev();
                                return 0;
                            }
                        }
                    }
                }
            }

    }

    public int mxAutoGetImage(byte[] bImgBuf, int iImgW, int iImgH, int iTimeOut, int iFlagLeave) {
        boolean iRet = false;
        MXLog.SendMsg("打开设备");

            MXLog.SendMsg("探测手指，红灯亮");
          int  iRet1 = this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_ON);
            if (iRet1 != 0) {
                this.m_usbBase.closeDev();
                return iRet1;
            } else {
                MXLog.SendMsg("开启FPC传感器");
                iRet1 = this.zzStartFPC();
                if (iRet1 != 0) {
                    this.m_usbBase.closeDev();
                    return iRet1;
                } else {
                    this.m_bCancelGetImage = false;
                    long duration = -1L;
                    Calendar time1 = Calendar.getInstance();
                    MXLog.SendMsg("探测手指");
                    iRet1 = this.zzDetectFinger();

                    Calendar time2;
                    while (iRet1 == 1 && duration <= (long) iTimeOut && !this.m_bCancelGetImage) {
                        iRet1 = this.zzDetectFinger();
                        SystemClock.sleep(10L);
                        time2 = Calendar.getInstance();
                        if (iTimeOut != 0) {
                            duration = time2.getTimeInMillis() - time1.getTimeInMillis();
                        }
                    }

                    byte iRet2;
                    if (this.m_bCancelGetImage) {
                        this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                        this.zzStopFPC();
                        this.m_usbBase.closeDev();
                        iRet2 = -2;
                        return iRet2;
                    } else if (duration > (long) iTimeOut) {
                        this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                        this.zzStopFPC();
                        this.m_usbBase.closeDev();
                        iRet2 = -3;
                        return iRet2;
                    } else {
                        iRet1 = this.zzUpAutoImage(bImgBuf, iImgW, iImgH, Boolean.valueOf(true));
                        if (iRet1 != 0) {
                            this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                            this.zzStopFPC();
                            this.m_usbBase.closeDev();
                            iRet2 = -5;
                            return iRet2;
                        } else {
                            MXLog.SendMsg("探测到手指，红灯灭");
                            this.zzLampOnOff(MXCommand.LED_RED_LIGHT, MXCommand.LED_OFF);
                            if (iFlagLeave == 1) {
                                this.zzLampOnOff(MXCommand.LED_GREEN_LIGHT, MXCommand.LED_ON);
                                duration = -1L;
                                time1 = Calendar.getInstance();
                                iRet1 = this.zzDetectFinger();

                                while (iRet1 == 0 && duration <= (long) iTimeOut && !this.m_bCancelGetImage) {
                                    iRet1 = this.zzDetectFinger();
                                    SystemClock.sleep(10L);
                                    time2 = Calendar.getInstance();
                                    if (iTimeOut != 0) {
                                        duration = time2.getTimeInMillis() - time1.getTimeInMillis();
                                    }
                                }

                                this.zzLampOnOff(MXCommand.LED_GREEN_LIGHT, MXCommand.LED_OFF);
                                if (this.m_bCancelGetImage) {
                                    this.zzStopFPC();
                                    this.m_usbBase.closeDev();
                                    iRet2 = -2;
                                    return iRet2;
                                }

                                if (duration > (long) iTimeOut) {
                                    this.zzStopFPC();
                                    this.m_usbBase.closeDev();
                                    iRet2 = -3;
                                    return iRet2;
                                }
                            }

                            MXLog.SendMsg("关闭FPC传感器");
                            this.zzStopFPC();
                            MXLog.SendMsg("关闭设备");
                            this.m_usbBase.closeDev();
                            return 0;
                        }
                    }
                }
            }

    }

    public int mxDevUpdate(byte[] bCodbuffer, int codbufferlen) {
        boolean iRet = false;
        byte[] checkStr = new byte[]{77, 46, 73, 46, 88, 46};
        byte[] checkcodStr = new byte[]{77, 105, 97, 120, 105, 115, 73, 68};

        for (int bCmd = 0; bCmd < 8; ++bCmd) {
            if (bCodbuffer[bCmd] != checkcodStr[bCmd]) {
                return -14;
            }
        }

        byte var22 = MXCommand.CMD_DEV_UPDATE;

            int iSendPackageSize = this.m_usbBase.sendPacketSize();
            int packsize = iSendPackageSize - 16;
            boolean packnum = false;
            boolean lasepacksize = false;
            int var23 = (codbufferlen - 8) / packsize;
            int var24 = (codbufferlen - 8) % packsize;
            if (var24 != 0) {
                ++var23;
            }

            for (int i = 0; i < var23; ++i) {
                boolean iSendLen = false;
                byte[] bSendBuf = new byte[iSendPackageSize];
                short num = (short) (i + 1);
                byte offsize = 0;
                int var26 = offsize + 1;
                bSendBuf[offsize] = (byte) (num & 255);
                bSendBuf[var26++] = (byte) (num >> 8);

                int bResult;
                for (bResult = 0; bResult < checkStr.length; ++bResult) {
                    bSendBuf[var26 + bResult] = checkStr[bResult];
                }

                var26 += checkStr.length;
                int var25 = var26 + packsize;
                if (i == var23 - 1) {
                    for (bResult = 0; bResult < var24; ++bResult) {
                        bSendBuf[var26 + bResult] = bCodbuffer[8 + i * packsize + bResult];
                    }
                } else {
                    for (bResult = 0; bResult < packsize; ++bResult) {
                        bSendBuf[var26 + bResult] = bCodbuffer[8 + i * packsize + bResult];
                    }
                }

               int var21 = this.sendPacket(var22, bSendBuf, var25);
                if (var21 != 0) {
                    this.m_usbBase.closeDev();
                    return var21;
                }

                byte[] var28 = new byte[1];
                int[] iRecvDataLen = new int[1];
                int iRecvPackageSize = this.m_usbBase.recvPacketSize();
                byte[] bRecvBuf = new byte[iRecvPackageSize];
                var21 = this.recvPacket(var28, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT * 3);
                if (var21 != 0) {
                    this.m_usbBase.closeDev();
                    return var21;
                }
            }

            this.m_usbBase.closeDev();
            return 0;

    }

    public void mxCancelGetImage() {
        this.m_bCancelGetImage = true;
    }


    public int zzCloseDev() {
        return this.m_usbBase.closeDev();
    }

    private int zzLampOnOff(int iLamp, int iOnOff) {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_LED_CONTROL;
        byte iSendLen = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        int var13 = iSendLen + 1;
        bSendBuf[iSendLen] = (byte) iLamp;
        bSendBuf[var13++] = (byte) iOnOff;
        int var12 = this.sendPacket(bCmd, bSendBuf, var13);
        if (var12 != 0) {
            return var12;
        } else {
            byte[] bResult = new byte[1];
            int[] iRecvDataLen = new int[1];
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            var12 = this.recvPacket(bResult, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT);
            return var12 != 0 ? var12 : 0;
        }
    }

    public int zzStartFPC() {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_START_FPC;
        byte iSendLen = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        int iRet1 = this.sendPacket(bCmd, bSendBuf, iSendLen);
        if (iRet1 != 0) {
            return iRet1;
        } else {
            byte[] bResult = new byte[1];
            int[] iRecvDataLen = new int[1];
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            iRet1 = this.recvPacket(bResult, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT);
            return iRet1 != 0 ? iRet1 : 0;
        }
    }

    public int zzStopFPC() {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_STOP_FPC;
        byte iSendLen = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        int iRet1 = this.sendPacket(bCmd, bSendBuf, iSendLen);
        if (iRet1 != 0) {
            return iRet1;
        } else {
            byte[] bResult = new byte[1];
            int[] iRecvDataLen = new int[1];
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            iRet1 = this.recvPacket(bResult, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT);
            return iRet1 != 0 ? iRet1 : 0;
        }
    }

    private int zzDetectFinger() {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_CHECK_FPC;
        byte iSendLen = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        int iRet1 = this.sendPacket(bCmd, bSendBuf, iSendLen);
        if (iRet1 != 0) {
            return iRet1;
        } else {
            byte[] bResult = new byte[1];
            int[] iRecvDataLen = new int[1];
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            iRet1 = this.recvPacket(bResult, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT);
            return iRet1 != 0 ? iRet1 : (bResult[0] == 0 ? 0 : 1);
        }
    }

    private int zzReadImage() {
        return 0;
    }

    private int zzUpImage(byte[] bImgBuf, int iImgLen) {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_GET_IMAGE;
        byte iSendLen = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        int var15 = this.sendPacket(bCmd, bSendBuf, iSendLen);
        if (var15 != 0) {
            MXLog.SendMsg("zzUpImage sendPacket iRet=" + var15);
            return var15;
        } else {
            byte[] bResult = new byte[1];
            int[] iRecvDataLen = new int[1];
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            var15 = this.recvPacket(bResult, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT);
            if (var15 != 0) {
                MXLog.SendMsg("zzUpImage recvPacket iRet=" + var15);
                return var15;
            } else {
                boolean iLength = false;

                for (int i = 0; i < iImgLen; i += iRecvPackageSize) {
                    int var16 = iImgLen - i;
                    if (var16 > iRecvPackageSize) {
                        var16 = iRecvPackageSize;
                    }

                    var15 = this.m_usbBase.recvData(bRecvBuf, bRecvBuf.length, MXCommand.CMD_TIMEOUT);
                    if (var15 < 0) {
                        MXLog.SendMsg("zzUpImage iImgLen=" + iImgLen);
                        MXLog.SendMsg("zzUpImage iRecvPackageSize=" + iRecvPackageSize);
                        MXLog.SendMsg("zzUpImage iLength=" + var16);
                        MXLog.SendMsg("zzUpImage i=" + i);
                        MXLog.SendMsg("zzUpImage iRet=" + var15);
                        return var15;
                    }

                    for (int j = 0; j < var16; ++j) {
                        bImgBuf[i + j] = bRecvBuf[j];
                    }
                }

                return 0;
            }
        }
    }

    public int zzUpAutoImage(byte[] bImgBuf, int iImgW, int iImgH, Boolean bOpenClose) {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_GET_AUTO_IMAGE;
        if (bOpenClose.booleanValue()) {
            bCmd = MXCommand.CMD_GET_AUTO_IMAGE_OC;
        }

        byte iSendLen = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        int var22 = this.sendPacket(bCmd, bSendBuf, iSendLen);
        if (var22 != 0) {
            MXLog.SendMsg("sendPacket=" + var22);
            return var22;
        } else {
            byte[] bResult = new byte[1];
            int[] iRecvDataLen = new int[1];
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            var22 = this.recvPacket(bResult, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT);
            if (var22 != 0) {
                MXLog.SendMsg("recvPacket=" + var22);
                return var22;
            } else {
                int a = bRecvBuf[2];
                if (a < 0) {
                    a += 256;
                }

                int b = bRecvBuf[3];
                if (b < 0) {
                    b += 256;
                }

                int wImgW = b * 256 + a;
                if (wImgW != iImgW) {
                    return -16;
                } else {
                    a = bRecvBuf[4];
                    if (a < 0) {
                        a += 256;
                    }

                    b = bRecvBuf[5];
                    if (b < 0) {
                        b += 256;
                    }

                    int wImgH = b * 256 + a;
                    if (wImgH != iImgH) {
                        return -17;
                    } else {
                        int reallen = wImgW * wImgH;
                        boolean iLength = false;

                        for (int i = 0; i < reallen; i += iRecvPackageSize) {
                            int var23 = reallen - i;
                            if (var23 > iRecvPackageSize) {
                                var23 = iRecvPackageSize;
                            }

                            var22 = this.m_usbBase.recvData(bRecvBuf, bRecvBuf.length, MXCommand.CMD_TIMEOUT);
                            if (var22 < 0) {
                                return var22;
                            }

                            for (int j = 0; j < var23; ++j) {
                                bImgBuf[i + j] = bRecvBuf[j];
                            }
                        }

                        return 0;
                    }
                }
            }
        }
    }

    private int sendPacket(byte bCmd, byte[] bSendBuf, int iDataLen) {
        boolean iRet = true;
        byte offsize = 0;
        short iCheckSum = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] DataBuffer = new byte[iSendPackageSize];
        int var12 = offsize + 1;
        DataBuffer[offsize] = MXCommand.CMD_REQ_FLAG;
        DataBuffer[var12++] = 0;
        DataBuffer[var12++] = 0;
        DataBuffer[var12++] = (byte) (iDataLen + 1 & 255);
        DataBuffer[var12++] = (byte) (iDataLen + 1 >> 8);
        DataBuffer[var12++] = bCmd;
        int i;
        if (iDataLen > 1) {
            for (i = 0; i < iDataLen; ++i) {
                DataBuffer[var12++] = bSendBuf[i];
            }
        }

        for (i = 3; i < var12; ++i) {
            byte iTmp = DataBuffer[i];
            iCheckSum = (short) (iCheckSum + this.JUnsigned(iTmp));
        }

        DataBuffer[var12++] = (byte) (iCheckSum & 255);
        DataBuffer[var12++] = (byte) (iCheckSum >> 8);
        this.m_usbBase.clearBuffer();
        int var11 = this.m_usbBase.sendData(DataBuffer, DataBuffer.length, MXCommand.CMD_TIMEOUT);
        return var11 < 0 ? -1 : 0;
    }

    private int recvPacket(byte[] bResult, byte[] bRecvBuf, int[] iRecvDataLen, int iTimeOut) {
        boolean iRet = true;
        byte offsize = 0;
        boolean iDataLen = false;
        boolean a = false;
        boolean b = false;
        int iRecvPackageSize = this.m_usbBase.recvPacketSize();
        byte[] DataBuffer = new byte[iRecvPackageSize];
        byte[] SRN = new byte[2];
        boolean recvCheckSum = false;
        short currentCheckSum = 0;
        int var16 = this.m_usbBase.recvData(DataBuffer, DataBuffer.length, MXCommand.CMD_TIMEOUT);
        if (var16 < 0) {
            MXLog.SendMsg("recvData=" + var16);
            return var16;
        } else {
            int var17 = offsize + 1;
            if (DataBuffer[offsize] != MXCommand.CMD_RET_FLAG) {
                return -12;
            } else {
                SRN[0] = DataBuffer[var17++];
                SRN[1] = DataBuffer[var17++];
                int var19 = DataBuffer[var17++];
                if (var19 < 0) {
                    var19 += 256;
                }

                int var20 = DataBuffer[var17++];
                if (var20 < 0) {
                    var20 += 256;
                }

                int var18 = var20 * 256 + var19;
                if (var18 > iRecvPackageSize - 5) {
                    return -15;
                } else {
                    bResult[0] = DataBuffer[var17];
                    iRecvDataLen[0] = var18 - 1;
                    int i;
                    if (var18 - 1 > 0) {
                        for (i = 1; i < var18; ++i) {
                            bRecvBuf[i - 1] = DataBuffer[var17 + i];
                        }
                    }

                    var17 += var18;

                    for (i = 3; i < var17; ++i) {
                        var19 = DataBuffer[i];
                        if (var19 < 0) {
                            var19 += 256;
                        }

                        currentCheckSum = (short) (currentCheckSum + var19);
                    }

                    var19 = DataBuffer[var17++];
                    if (var19 < 0) {
                        var19 += 256;
                    }

                    var20 = DataBuffer[var17++];
                    if (var20 < 0) {
                        var20 += 256;
                    }

                    short var21 = (short) (var20 * 256 + var19);
                    if (currentCheckSum != var21) {
                        return -14;
                    } else {
                        return 0;
                    }
                }
            }
        }
    }

    private int zzUpImageAndroid(byte[] bImgBuf, int[] iImgWidth, int[] iImgHeight) {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_GET_IMAGE_ANDROID;
        byte iSendLen = 0;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        MXLog.SendMsg("发送数据包");
        int var26 = this.sendPacket(bCmd, bSendBuf, iSendLen);
        if (var26 != 0) {
            MXLog.SendMsg("zzUpImage sendPacket iRet=" + var26);
            return var26;
        } else {
            byte[] bResult = new byte[1];
            int[] iRecvDataLen = new int[1];
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            MXLog.SendMsg("接收应答包");
            var26 = this.recvPacket(bResult, bRecvBuf, iRecvDataLen, MXCommand.CMD_TIMEOUT);
            if (var26 != 0) {
                MXLog.SendMsg("zzUpImage recvPacket iRet=" + var26);
                return var26;
            } else {
                boolean a = false;
                boolean b = false;
                int var27 = this.JUnsigned(bRecvBuf[0]);
                int var28 = this.JUnsigned(bRecvBuf[1]);
                int iPackCount = var28 * 256 + var27;
                var27 = this.JUnsigned(bRecvBuf[2]);
                var28 = this.JUnsigned(bRecvBuf[3]);
                iImgWidth[0] = var28 * 256 + var27;
                var27 = this.JUnsigned(bRecvBuf[4]);
                var28 = this.JUnsigned(bRecvBuf[5]);
                iImgHeight[0] = var28 * 256 + var27;
                MXLog.SendMsg("包数:" + iPackCount);
                MXLog.SendMsg("宽度:" + iImgWidth[0]);
                MXLog.SendMsg("高度:" + iImgHeight[0]);
                byte[] bImgBufTmp = new byte[iImgWidth[0] * iImgHeight[0] + 100];
                int[] iPackNumArray = new int[iPackCount];
                int[] iPackLenArray = new int[iPackCount];

                int i;
                for (i = 0; i < iPackNumArray.length; ++i) {
                    iPackNumArray[i] = 0;
                    iPackLenArray[i] = 0;
                }

                MXLog.SendMsg("接收数据包");
                boolean var29 = false;
                int iLength = 0;

                for (i = 0; i < iPackCount; ++i) {
                    iLength = iRecvPackageSize;
                    if (i == iPackCount - 1) {
                        iLength = iImgWidth[0] * iImgHeight[0] - i * (iRecvPackageSize - 2) + 2;
                    }

                    var26 = this.m_usbBase.recvData(bRecvBuf, bRecvBuf.length, MXCommand.CMD_TIMEOUT);
                    if (var26 < 0) {
                        break;
                    }

                    var27 = this.JUnsigned(bRecvBuf[0]);
                    var28 = this.JUnsigned(bRecvBuf[1]);
                    iPackNumArray[i] = var28 * 256 + var27;
                    iPackLenArray[i] = iLength - 2;
                    int strTmp = iPackNumArray[i] * (iRecvPackageSize - 2);
                    System.arraycopy(bRecvBuf, 2, bImgBufTmp, strTmp, iLength - 2);
                }

                String var30 = "收到包序号：";

                int k;
                for (k = 0; k < iPackCount; ++k) {
                    var30 = var30 + "[" + iPackNumArray[k] + "] ";
                }

                MXLog.SendMsg(var30);
                if (var26 < 0) {
                    MXLog.SendMsg("zzUpImage iRecvPackageSize=" + iRecvPackageSize);
                    MXLog.SendMsg("zzUpImage i=" + i);
                    MXLog.SendMsg("zzUpImage iLength=" + iLength);
                    MXLog.SendMsg("zzUpImage iRet=" + var26);
                    k = 0;
                    int iLossPackNum = 0;
                    int[] iLossPackNumArray = new int[iPackCount - i];

                    int iDstPos;
                    for (iDstPos = 0; iDstPos < i; ++iDstPos) {
                        if (iPackNumArray[k] != iDstPos) {
                            iLossPackNumArray[iLossPackNum] = iDstPos;
                            ++iLossPackNum;
                        } else {
                            ++k;
                        }
                    }

                    MXLog.SendMsg("丢包个数=" + iLossPackNum);
                    var30 = "丢包序号：\r\n";

                    for (k = 0; k < iLossPackNum; ++k) {
                        var30 = var30 + "[" + iLossPackNumArray[k] + "] ";
                    }

                    if (iLossPackNum > 20) {
                        return -20;
                    }

                    for (k = 0; k < iLossPackNum; ++k) {
                        var26 = this.zzUpPackAndroid(bRecvBuf, iLossPackNumArray[k]);
                        if (var26 != 0) {
                            break;
                        }

                        iLength = iRecvPackageSize;
                        if (iLossPackNumArray[k] == iPackCount - 1) {
                            iLength = iImgWidth[0] * iImgHeight[0] - i * (iRecvPackageSize - 2);
                        }

                        iDstPos = iLossPackNumArray[k] * (iRecvPackageSize - 2);
                        System.arraycopy(bRecvBuf, 0, bImgBufTmp, iDstPos, iLength);
                    }
                }

                System.arraycopy(bImgBufTmp, 0, bImgBuf, 0, iImgWidth[0] * iImgHeight[0]);
                return 0;
            }
        }
    }

    private int zzUpPackAndroid(byte[] bDataBuf, int iPackNum) {
        boolean iRet = false;
        byte bCmd = MXCommand.CMD_GET_PACK_ANDROID;
        byte iSendLen = 2;
        int iSendPackageSize = this.m_usbBase.sendPacketSize();
        byte[] bSendBuf = new byte[iSendPackageSize];
        bSendBuf[0] = (byte) iPackNum;
        bSendBuf[1] = (byte) (iPackNum >> 8);
        MXLog.SendMsg("获取第[" + iPackNum + "]包图像数据");
        int iRet1 = this.sendPacket(bCmd, bSendBuf, iSendLen);
        if (iRet1 != 0) {
            MXLog.SendMsg("zzUpPackAndroid sendPacket iRet=" + iRet1);
            return iRet1;
        } else {
            int iRecvPackageSize = this.m_usbBase.recvPacketSize();
            byte[] bRecvBuf = new byte[iRecvPackageSize];
            iRet1 = this.m_usbBase.recvData(bRecvBuf, bRecvBuf.length, 100);
            if (iRet1 < 0) {
                return iRet1;
            } else {
                int a = this.JUnsigned(bRecvBuf[0]);
                int b = this.JUnsigned(bRecvBuf[1]);
                if (iPackNum != b * 256 + a) {
                    MXLog.SendMsg("iRet=" + iRet1);
                    MXLog.SendMsg("收到包序号=" + (b * 256 + a));
                    MXLog.SendMsg(zzStringTrans.hex2str(bRecvBuf));
                    return 88;
                } else {
                    System.arraycopy(bRecvBuf, 2, bDataBuf, 0, iRet1 - 2);
                    return 0;
                }
            }
        }
    }

    public void MySleep(int iTimeout) {
        long duration = -1L;

        Calendar time2;
        for (Calendar time1 = Calendar.getInstance(); duration <= (long) iTimeout; duration = time2.getTimeInMillis() - time1.getTimeInMillis()) {
            time2 = Calendar.getInstance();
        }

    }

    public void unRegUsbMonitor() {
        this.m_usbBase.unRegisterMonitor();
    }

    public int JUnsigned(int x) {
        return x >= 0 ? x : x + 256;
    }
}
